package com.ruoyi.service.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.service.utils.ServiceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.service.domain.Trade;
import com.ruoyi.service.service.ITradeService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.common.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;

/**
 * 基本电价及直接交易 信息操作处理
 *
 * @author wangbing
 * @date 2019-01-03
 */
@Controller
@RequestMapping("/service/trade")
public class TradeController extends BaseController {
    private String prefix = "service/trade";

    @Autowired
    private ITradeService tradeService;
    @Autowired
    private ServiceUtils utils;

    @GetMapping()
    public String trade() {
        return prefix + "/trade";
    }

    /**
     * 查询基本电价及直接交易列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Trade trade) {
        startPage();
        List<Trade> list = tradeService.selectTradeList(trade);
        return getDataTable(list);
    }


    /**
     * 导出基本电价及直接交易列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Trade trade) {
        List<Trade> list = tradeService.selectTradeList(trade);
        ExcelUtil<Trade> util = new ExcelUtil<Trade>(Trade.class);
        return util.exportExcel(list, "trade");
    }

    /**
     * 新增基本电价及直接交易
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存基本电价及直接交易
     */
    @Log(title = "基本电价及直接交易", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Trade trade) {
        return toAjax(tradeService.insertTrade(trade));
    }

    /**
     * 修改基本电价及直接交易
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Trade trade = tradeService.selectTradeById(id);
        mmap.put("trade", trade);
        mmap.put("month", utils.getMonth(trade.getBasicsId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存基本电价及直接交易
     */
    @Log(title = "基本电价及直接交易", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Trade trade) {

        trade.setBasicsCost(trade.getBasicsPrice().multiply(trade.getAverageVolume()));
        trade.setFTotal(trade.getFF().add(trade.getFP()).add(trade.getFG()));
        trade.setPTotal(trade.getPF().add(trade.getPP()).add(trade.getPG()));
        trade.setKTotal(trade.getKF().add(trade.getKP()).add(trade.getKG()));
        trade.setTradeTotal(trade.getFTotal().add(trade.getPTotal()).add(trade.getKTotal()));
        trade.setUpCost(trade.getFactoryPrice().multiply(trade.getTradeTotal()));
        trade.setSPAccount(trade.getSPPrice().multiply(trade.getTradeTotal()));
        trade.setJJAcount(trade.getJJAverage().multiply(trade.getTradeTotal()));
//        trade.setEleAcount(trade.getDHAverage().multiply(trade.getTradeTotal()));
//        用户承担电费金额eleAcount = 上网电费upCost + 输配收入金额sPAccount
        trade.setEleAcount(trade.getUpCost().add(trade.getSPAccount()));
//        到户均价dHAverage = 用户承担电费金额eleAcount / 交易电量合计tradeTotal
        BigDecimal TradeTotal = trade.getTradeTotal();
        if (TradeTotal == BigDecimal.valueOf(0)) {
            trade.setDHAverage(BigDecimal.ZERO);
        }else{
            trade.setDHAverage(trade.getEleAcount().divide(TradeTotal).setScale(6,BigDecimal.ROUND_HALF_UP));
        }
        int i = tradeService.updateTrade(trade);
        utils.total(trade.getPointId(), trade.getBasicsId());
        return toAjax(i);
    }

    /**
     * 删除基本电价及直接交易
     */
    @Log(title = "基本电价及直接交易", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tradeService.deleteTradeByIds(ids));
    }

}
